package cn.lyjuan.android.demofive.ui.slide.flipper;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.ViewFlipper;
import cn.lyjuan.android.demofive.R;

/**
 * Created by chad on 2016/3/24.
 *
 * Flipper提供左右导航
 */
public class FlipperNavActivity extends Activity
{
    private ViewFlipper flipper;

    private ImageView left;
    private ImageView right;

    private boolean isHide = false;// 标识当前操作是隐藏还是显示，默认显示
    private int alpha = 100;

    private static final int HANDLER_WHAT_NAV_SHOW = 1;// 显示操作 标识
    private static final int HANDLER_WHAT_NAV_HIDE = 0;// 隐藏操作 标识

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);

        this.setContentView(R.layout.ui_flipper);

        this.flipper = (ViewFlipper) this.findViewById(R.id.ui_flipper);
        flipper.addView(addImageView(R.drawable.img_01));
        flipper.addView(addImageView(R.drawable.img_02));
        flipper.addView(addImageView(R.drawable.img_03));
        flipper.addView(addImageView(R.drawable.img_04));

        left = (ImageView) this.findViewById(R.id.ui_flipper_nav_left);
        right = (ImageView) this.findViewById(R.id.ui_flipper_nav_right);
        setNavImg(left, DirType.LEFT);
        setNavImg(right, DirType.RIGHT);

        hideNavImg();
    }

    /**
     * 添加图片View
     * @param layoutId
     * @return
     */
    private ImageView addImageView(int layoutId)
    {
        ImageView img = new ImageView(this);
        img.setImageResource(layoutId);
        return img;
    }

    /**
     * 设置导航按钮并绑定事件
     * @param dir
     * @return
     */
    private ImageView setNavImg(ImageView view, DirType dir)
    {
        view.setAlpha(this.alpha);

        if (dir == DirType.LEFT)// 左向
        {
            view.setImageResource(R.drawable.ui_flipper_left);

            view.setOnTouchListener(new View.OnTouchListener()
            {
                @Override
                public boolean onTouch(View v, MotionEvent event)
                {
                    if (MotionEvent.ACTION_DOWN == event.getAction())
                    {
                        showNavImg();
                    } else if (MotionEvent.ACTION_UP == event.getAction())
                    {
                        flipper.setInAnimation(FlipperNavActivity.this, R.anim.ui_flipper_right_in);
                        flipper.setOutAnimation(FlipperNavActivity.this, R.anim.ui_flipper_right_out);
                        flipper.showNext();
                        hideNavImg();
                    } else return false;

                    return true;
                }
            });

        } else// 右向
        {
            view.setImageResource(R.drawable.ui_flipper_right);

            view.setOnTouchListener(new View.OnTouchListener()
            {
                @Override
                public boolean onTouch(View v, MotionEvent event)
                {
                    if (MotionEvent.ACTION_DOWN == event.getAction())
                    {
                        showNavImg();
                    } else if (MotionEvent.ACTION_UP == event.getAction())
                    {
                        flipper.setInAnimation(FlipperNavActivity.this, R.anim.ui_flipper_left_in);
                        flipper.setOutAnimation(FlipperNavActivity.this, R.anim.ui_flipper_left_out);
                        flipper.showPrevious();
                        hideNavImg();
                    } else return false;

                    return true;
                }
            });
        }

        return view;
    }

    /**
     * 监听触摸事件
     * @param event
     * @return
     */
    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        switch (event.getAction())
        {
            case MotionEvent.ACTION_MOVE :// case穿透，移动操作同按下操作
            case MotionEvent.ACTION_DOWN :
                showNavImg();// 渐显导航按钮
                break;
            case MotionEvent.ACTION_UP :
                hideNavImg();// 渐隐导航按钮
                break;
        }

        return true;
    }

    /**
     * 渐显
     */
    private void showNavImg()
    {
        isHide = false;
        showHideHandler.sendEmptyMessage(HANDLER_WHAT_NAV_SHOW);
    }

    /**
     * 渐隐
     */
    private void hideNavImg()
    {
        new Thread(){
            @Override
            public void run()
            {
                try
                {
                    Thread.sleep(500);
                    isHide = true;
                    showHideHandler.sendEmptyMessage(HANDLER_WHAT_NAV_HIDE);
                } catch (InterruptedException e) {e.printStackTrace();}
            }
        }.start();
    }

    /**
     * 与主线程同步显示隐藏导航按钮
     */
    private Handler showHideHandler = new Handler()
    {
        @Override
        public void handleMessage(Message msg)
        {
            if (HANDLER_WHAT_NAV_SHOW == msg.what && alpha < 255)// 渐显，每次增加50个不透明度
            {
                alpha += 50;

                if (alpha > 255) alpha = 255;

                if (!isHide && alpha < 255)
                    showHideHandler.sendEmptyMessageDelayed(HANDLER_WHAT_NAV_SHOW, 100);

            } else if (HANDLER_WHAT_NAV_HIDE == msg.what && alpha > 0) // 渐隐，每次减少 10 个不透明度
            {
                alpha -= 10;

                if (alpha < 0) alpha = 0;

                if (isHide && alpha > 0)
                    showHideHandler.sendEmptyMessageDelayed(HANDLER_WHAT_NAV_HIDE, 100);
            }

            left.setAlpha(alpha);
            left.invalidate();
            right.setAlpha(alpha);
            right.invalidate();
        }
    };

    enum DirType
    {
        LEFT, RIGHT;
    }
}
